package site.qjdb.service.impl;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import site.qjdb.dao.ReplyDao;
import site.qjdb.dao.WantDao;
import site.qjdb.pojo.Reply;
import site.qjdb.pojo.Want;
import site.qjdb.service.WantService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WantServiceImpl implements WantService {
	@Resource
	private WantDao wantDao;
	@Resource
	private ReplyDao replyDao;

	@Override
	public void addWant(Want want) {
		wantDao.addWant(want);
	}

	@Override
	public Want findWant(Want want) {
		Map<String, Object> map = new HashMap<>();
		map.put("want", want);
		List<Want> wantList = wantDao.selectWantAll(map);
		if(wantList == null || wantList.size() == 0) {
			return null;
		} else {
			return wantList.get(0);
		}
	}

	// TODO ++
	@Override
	public void addLikeCount(Want want) {
		if(want.getWid() == null) {
			return;
		}
		Integer likeCount = want.getLikeCount();
		if(likeCount == null) {
			likeCount = new Integer("0");
		}
		likeCount++;
		// 增加点赞数
		Want newWant = new Want();
		newWant.setWid(want.getWid());
		newWant.setLikeCount(likeCount);
		wantDao.updateWant(newWant);		
	}

	@Override
	public List<Want> findWantAll(Want want) {
		Map<String, Object> map = new HashMap<>();
		map.put("want", want);
		map.put("orderBy", "want.time desc,want.like_count desc");
		List<Want> wantList = wantDao.selectWantAll(map);
		if(wantList == null || wantList.size() == 0) {
			return null;
		} else {
			for(Want _want : wantList) {
				// 查询留言数
				Reply _reply = new Reply();
				// 类型：1（求购信息）
				_reply.setType(1);
				_reply.setIsDel(0);
				_reply.setSuperiorId(_want.getWid());
				_want.setReplyCount(replyDao.selectReplyCount(_reply));
			}
			return wantList;
		}
	}
}
